package com.sc.nft.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.DigestUtil;
import com.sc.nft.annotation.UserLogin;
import com.sc.nft.dao.StaticDataDao;
import com.sc.nft.entity.StaticData;
import com.sc.nft.pool.UserPool;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/welfarePrefecture")
@Api(tags = "福利专区")
public class WelfarePrefectureController {
    private final StaticDataDao staticDataDao;

    // 分销码
    private final static String secretKey = "aGphJymAxdz8RNA8y9vg6czujUcdpd2u";

    @PostMapping(value = "/welfarePrefectureH5", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "福利专区H5权益", notes = "福利专区H5权益", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result welfarePrefectureH5() {
        String url = encryption();
        Map map = new HashedMap();
        StaticData staticData = staticDataDao.getByType("welfare_prefecture");
        map.put("h5Url", StrUtil.format("{}?{}",staticData.getValue(),url));
        return Result.success(map);
    }

    public static String encryption() {
        long currentSeconds = DateUtil.currentSeconds();
        String info = "distribution_code=137dVsx&machine_code=" + UserPool.getUser().getId() + "&timestamp=" + currentSeconds + "&secretKey=" + secretKey;
        String md5 = DigestUtil.md5Hex(info).toUpperCase();
        info=info+"&sign="+md5;
        return info;
    }

}
